package com.ccl.iot.fragments.dialog;


import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.util.DisplayMetrics;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import com.ccl.iot.R;
import com.ccl.iot.dvr.utils.LogUtil;
import com.ccl.iot.huiju.MainActivity;


public abstract class BaseDialogFragment extends DialogFragment {

    protected static final String TAG = "DVR";
    protected static final String CHANNELNO = "channelNo";
    protected static final String IS_CANBACK = "isCanBack";

    protected int mDialogWidth;
    protected int mDialogHeight;
    /**
     * 改变DialogFragment大小的标记
     */
    protected boolean sizeFlag;

    public BaseDialogFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (sizeFlag) {
            DisplayMetrics dm = getResources().getDisplayMetrics();
            mDialogWidth = dm.widthPixels;
            mDialogHeight = dm.heightPixels;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (sizeFlag) {
            getDialog().getWindow().setLayout(mDialogWidth, mDialogHeight);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        //设置主题
        Context context = new ContextThemeWrapper(getActivity(), R.style.loginTheme);
        LayoutInflater localInflater = inflater.cloneInContext(context);

        View view = BindContentView(localInflater, container);
        initView(view);
        initListeners();
        initData();

        hideBottomUIMenu();

        View decorView = getDialog().getWindow().getDecorView();
        decorView.setOnSystemUiVisibilityChangeListener
                (new View.OnSystemUiVisibilityChangeListener() {
                    @Override
                    public void onSystemUiVisibilityChange(int visibility) {
                        // Note that system bars will only be "visible" if none of the
                        // LOW_PROFILE, HIDE_NAVIGATION, or FULLSCREEN flags are set.
                        if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
                            // TODO: The system bars are visible. Make any desired
                            // adjustments to your UI, such as showing the action bar or
                            // other navigational controls.
                            hideBottomUIMenu();
                        } else {
                            // TODO: The system bars are NOT visible. Make any desired
                            // adjustments to your UI, such as hiding the action bar or
                            // other navigational controls.
                        }
                    }
                });

        return view;
    }

    public abstract View BindContentView(LayoutInflater localInflater, ViewGroup container);

    public abstract void initView(View view);

    public abstract void initListeners();

    public abstract void initData();


    public void hideBottomUIMenu() {

        //隐藏虚拟按键，并且全屏
        if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
            View v = getDialog().getWindow().getDecorView();
            v.setSystemUiVisibility(View.GONE);
            LogUtil.i(TAG, "hideBottomUIMenu4");
        } else if (Build.VERSION.SDK_INT >= 19) {
            //for new api versions.
            View decorView = getDialog().getWindow().getDecorView();
            int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(uiOptions);
            LogUtil.i(TAG, "hideBottomUIMenu5");
        }

    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();

        if (getActivity() instanceof MainActivity) {
            ((MainActivity)getActivity()).hideBottomUIMenu();
        }

        LogUtil.e(MainActivity.TAG,"onDestroyView-----------");
    }
}
